package com.bg.print.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bg.print.dao.MaterialDao;
import com.bg.print.entity.MaterialEntity;
import com.bg.print.service.MaterialService;
import com.bg.print.utils.Constant;
import com.bg.print.utils.PageUtils;
import com.bg.print.utils.Query;
import com.bg.print.variable.MaterialValue;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service("materialService")
public class MaterialServiceImpl extends ServiceImpl<MaterialDao, MaterialEntity> implements MaterialService {
    //查询外放
    @Override
    public PageUtils queryUserPage(Map<String, Object> params) {
        LambdaQueryWrapper<MaterialEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialEntity::getIsListed, MaterialValue.isListed.GROUNDING.getCode());
        String name= (String) params.get(Constant.MATERIAL_NAME);
        if(StringUtils.hasText(name)){
            queryWrapper.like(MaterialEntity::getName, name);
        }

        IPage<MaterialEntity> page = this.page(
                new Query<MaterialEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


    //查询不外放
    @Override
    public PageUtils queryPages(Map<String, Object> params) {
        LambdaQueryWrapper<MaterialEntity> queryWrapper = new LambdaQueryWrapper<>();
        String name= (String) params.get(Constant.MATERIAL_NAME);
        if(StringUtils.hasText(name)){
            queryWrapper.like(MaterialEntity::getName, name);
        }

        IPage<MaterialEntity> page = this.page(
                new Query<MaterialEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 更改有无货
     * @param id
     * @param a
     * @param b
     * @return
     */
    @Override
    public Integer updataStockStatus(Long id, Integer a, Integer b) {
        LambdaUpdateWrapper<MaterialEntity> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(MaterialEntity::getId,id);
        queryWrapper.eq(MaterialEntity::getStockStatus,a);
        queryWrapper.set(MaterialEntity::getStockStatus,b);
        return this.baseMapper.update(null,queryWrapper);
    }

    @Override
    public Integer updataIsListed(Long id, Integer code, Integer code1) {
        LambdaUpdateWrapper<MaterialEntity> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(MaterialEntity::getId,id);
        queryWrapper.eq(MaterialEntity::getIsListed,code);
        queryWrapper.set(MaterialEntity::getIsListed,code1);
        return this.baseMapper.update(null,queryWrapper);
    }


}